#!/bin/bash

set -e

if [ "$1" = "configure" ]; then

	# Create the "puppet" user
	if ! getent passwd puppet > /dev/null; then
		useradd --system --user-group --home-dir /var/lib/puppet \
			--no-create-home --shell /bin/false \
			--comment "Puppet configuration management daemon" \
			puppet
	fi

	# Set correct permissions and ownership for puppet directories
	for dir in /var/{run,lib,log}/puppet; do
		if ! dpkg-statoverride --list "$dir" >/dev/null 2>&1; then
			dpkg-statoverride --update --add puppet puppet 0750 "$dir"
		fi
	done

	# Create folders common to "puppet" and "puppetmaster", which need
	# to be owned by the "puppet" user
	install --owner puppet --group puppet --directory \
		/var/lib/puppet/state
	install --owner puppet --group puppet --directory \
		/var/lib/puppet/reports
	
	# Handle 
	if [ -d /etc/puppet/ssl ] && [ ! -e /var/lib/puppet/ssl ] && grep -q 'ssldir=/var/lib/puppet/ssl' /etc/puppet/puppet.conf; then
		mv /etc/puppet/ssl /var/lib/puppet/ssl
	fi
fi

#DEBHELPER#
